//
//  NSObject+swizzle.m
//  YDGJ
//
//  Created by Luke on 16/6/13.
//  Copyright © 2016年 Galaxy360. All rights reserved.
//

#import <objc/runtime.h>


//#if DEBUG
//
//#else
//
//#endif

//
///**
// *  兼容数组,字典的一些异常操作
// */
//@implementation NSObject (swizzle)
//
///**
// *  交换两个类方法的实现
// *
// *  @param class          类名
// *  @param originSelector 原始方法
// *  @param otherSelector  需要覆盖原始的方法
// */
//+ (void)swizzleClassMethod:(Class)class originSelector:(SEL)originSelector otherSelector:(SEL)otherSelector
//{
//    Method otherMehtod = class_getClassMethod(class, otherSelector);
//    Method originMehtod = class_getClassMethod(class, originSelector);
//    method_exchangeImplementations(otherMehtod, originMehtod);
//}
//
///**
// *  交换两个实例方法的实现
// *
// *  @param class          类名
// *  @param originSelector 原始方法
// *  @param otherSelector  需要覆盖原始的方法
// */
//+ (void)swizzleInstanceMethod:(Class)class originSelector:(SEL)originSelector otherSelector:(SEL)otherSelector
//{
//    Method otherMehtod = class_getInstanceMethod(class, otherSelector);
//    Method originMehtod = class_getInstanceMethod(class, originSelector);
//    method_exchangeImplementations(otherMehtod, originMehtod);
//}
//
//@end
//
///**
// *  防止数组越界访问
// */
//@implementation NSArray(Extension)
//
//+ (void)load
//{
//    [self swizzleInstanceMethod:NSClassFromString(@"__NSArrayI") originSelector:@selector(objectAtIndex:) otherSelector:@selector(gj_objectAtIndex:)];
//}
//
//- (id)gj_objectAtIndex:(NSUInteger)index
//{
//    if (index < self.count) {
//        return [self gj_objectAtIndex:index];
//    } else {
//        return nil;
//    }
//}
//
//@end
//
//
///**
// *  防止可变数组越界访问, 添加空对象
// */
//@implementation NSMutableArray(Extension)
//+ (void)load
//{
//    [self swizzleInstanceMethod:NSClassFromString(@"__NSArrayM") originSelector:@selector(addObject:) otherSelector:@selector(gj_addObject:)];
//    [self swizzleInstanceMethod:NSClassFromString(@"__NSArrayM") originSelector:@selector(objectAtIndex:) otherSelector:@selector(gj_objectAtIndex:)];
//}
//
//- (void)gj_addObject:(id)object
//{
//    if (object != nil) {
//        [self gj_addObject:object];
//    }
//}
//
//- (id)gj_objectAtIndex:(NSUInteger)index
//{
//    if (index < self.count) {
//        return [self gj_objectAtIndex:index];
//    } else {
//        return nil;
//    }
//}
//
//@end
//
///**
// *  防止可变字典设置空对象
// */
//@implementation NSMutableDictionary(Extension)
//
//+ (void)load
//{
//    [self swizzleInstanceMethod:NSClassFromString(@"__NSDictionaryM") originSelector:@selector(setObject:forKey:) otherSelector:@selector(gj_SetObject:forKey:)];
//}
//
//- (void)gj_SetObject:(id)anObject forKey:(id <NSCopying>)aKey
//{
//    if (!anObject) return;
//
//    [self gj_SetObject:anObject forKey:aKey];
//}
//
//@end

